package com.moleq.itemsetting.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;

import com.moleq.models.Functions;
import com.moleq.models.GlobalInfo;
import com.moleq.models.Users;
import com.moleq.mgpos.activities.R;

public class AppChooseAdapterIty extends BaseAdapter {

	private LayoutInflater mInflater;
	private List<Functions> listItem;
	private Users user;
	private int selectedRow = -1;
	private OnClickAppChooseItem mOnClickAppChooseItem;
	private GlobalInfo mGlobalInfo;
	private Context mContext;
	
	private Drawable img_inventory,img_receiving,img_receiving_inquiry;

	public AppChooseAdapterIty(Context context, List<Functions> listItem,
			Users user) {
		this.mInflater = LayoutInflater.from(context);
		this.listItem = listItem;
		this.mContext = context;
		this.user = user;
		img_inventory = mContext.getResources().getDrawable(R.drawable.choose_sub_inventory);
		img_receiving = mContext.getResources().getDrawable(R.drawable.choose_receiving);
		img_receiving_inquiry = mContext.getResources().getDrawable(R.drawable.choose_receiving_inquiry);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return listItem.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return listItem.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		mGlobalInfo = GlobalInfo.getInstance();
		Drawable drawable = null;
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.app_choose_items, null);
			holder.btn_app_choose_item = (Button) convertView
					.findViewById(R.id.btn_app_choose);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.btn_app_choose_item.setText(listItem.get(position).getFs_name());
		holder.btn_app_choose_item.setTag(listItem.get(position).getFs_id());
		holder.btn_app_choose_item.setOnClickListener(mOnClickListener);
		if (listItem.get(position).getFs_name().equals("Receiving")) {
			holder.btn_app_choose_item.setCompoundDrawablesWithIntrinsicBounds(
					img_receiving, null, null, null);
		}else if (listItem.get(position).getFs_name().equals("Receiving Inquiry")) {
			holder.btn_app_choose_item.setCompoundDrawablesWithIntrinsicBounds(
					img_receiving_inquiry, null, null, null);
		}else if (listItem.get(position).getFs_name().equals("Inventory")) {
			holder.btn_app_choose_item.setCompoundDrawablesWithIntrinsicBounds(
					img_inventory, null, null, null);
		}
		
		if (user.getUs_color() == 1 || mGlobalInfo.cashier.equals("8888")) {
			drawable = mContext.getResources().getDrawable(R.drawable.button_1);
		} else if (user.getUs_color() == 2) {
			drawable = mContext.getResources().getDrawable(R.drawable.button_2);
		} else if (user.getUs_color() == 3) {
			drawable = mContext.getResources().getDrawable(R.drawable.button_3);
		} else if (user.getUs_color() == 4) {
			drawable = mContext.getResources().getDrawable(R.drawable.button_4);
		} else if (user.getUs_color() == 5) {
			drawable = mContext.getResources().getDrawable(R.drawable.button_5);
		} else {
			drawable = mContext.getResources().getDrawable(R.drawable.button_1);
		}
		holder.btn_app_choose_item.setBackgroundDrawable(drawable);
		return convertView;
	}

	public OnClickListener mOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			int id = (Integer) v.getTag();
			mOnClickAppChooseItem.onClickItem(id);
		}

	};

	public final class ViewHolder {
		public Button btn_app_choose_item;
	}

	public void setOnClickMessageListener(OnClickAppChooseItem listener) {
		mOnClickAppChooseItem = listener;
	}

	public interface OnClickAppChooseItem {
		public void onClickItem(int id);
	}
}
